package licode.unisop.client.feign.impl;

import licode.unisop.client.api.SopPlatform;
import licode.unisop.client.api.SopPlatformAuth;
import licode.unisop.client.api.SopPlatformMng;
import licode.unisop.client.api.auth.SopUserAuth;
import licode.unisop.client.info.*;
import licode.unisop.client.utils.SopRequestParse;
import licode.unisop.client.utils.StringUtil;
import licode.unisop.client.visit.UniClientTool;
import licode.unisop.client.visit.UserAuthVisit;
import licode.unisop.client.vo.SopAuthContext;
import licode.unisop.client.vo.SopAuthResult;
import licode.unisop.client.vo.SopAuthUser;
import licode.unisop.client.vo.SopCredential;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * APP认证接口定义
 *
 * @author WR
 */
@Component
public class UserAuthVisitFeignImpl implements UserAuthVisit {
    @Resource
    private HttpServletRequest request;

    @Resource
    private SopUserAuth sopUserAuth;

    @Resource
    private UniClientTool uniClientTool;

    @Resource
    private SopPlatformMng platformMng;

    @Resource
    private SopPlatformAuth sopPlatformAuth;

    /**
     * 根据Code换取登录信息
     *
     * @return 返回认证后用户信息
     */
    @Override
    public SopAuthUser token(SopCodeInfo codeInfo) {
        if (StringUtil.isBlank(codeInfo.getPlatformId())) {
            codeInfo.setPlatformId(SopRequestParse.getPlatformId(request));
        }
        return sopUserAuth.token(codeInfo);
    }

    public void fillAuthInfo(SopAuthContext context) {
        if (request != null && null != context) {
            if (StringUtil.isBlank(context.getPlatformId())) {
                context.setPlatformId(SopRequestParse.getPlatformId(request));
            }
            if (StringUtil.isBlank(context.getRealmId())) {
                context.setRealmId(SopRequestParse.getRealmId(request));
            }
            if (StringUtil.isBlank(context.getTenantId())) {
                context.setTenantId(SopRequestParse.getTenantId(request));
            }
            if (StringUtil.isBlank(context.getTenantId())) {
                context.setTerminalCode(SopRequestParse.getTerminalId(request));
            }
            if (StringUtil.isBlank(context.getClientCode())) {
                context.setClientCode(SopRequestParse.getClientId(request));
            }
            if (StringUtil.isBlank(context.getClientId())) {
                context.setClientId(SopRequestParse.getClientId(request));
            }
            if (StringUtil.isBlank(context.getClientSecret())) {
                context.setClientSecret(SopRequestParse.getClientSecret(request));
            }
        }
    }

    public void handlePlatformInfo(SopAuthContext context) {
        SopPlatform plat = platformMng.getPlatform(context.getPlatformId());
        if (StringUtil.isBlank(context.getRealmId())) {
            context.setRealmId(plat.getRealmId());
        }
    }

    public void handleCaptcha(SopAuthIn authIn) {
        if (StringUtil.isBlank(authIn.getCaptchaKey())) {
            authIn.setCaptchaKey(SopRequestParse.getCaptchaKey(request));
            authIn.setCaptchaCode(SopRequestParse.getCaptchaCode(request));
        }
    }

    public void handleCaptcha(SopAuthDirectIn authIn) {
        if (StringUtil.isBlank(authIn.getCaptchaKey())) {
            authIn.setCaptchaKey(SopRequestParse.getCaptchaKey(request));
            authIn.setCaptchaCode(SopRequestParse.getCaptchaCode(request));
        }
    }

    /**
     * 用户登录认证
     *
     * @param authIn 认证用户信息
     * @return 返回认证后用户信息
     */
    public SopAuthUser login(SopAuthIn authIn) {
        if (null == authIn.getContext()) {
            authIn.setContext(new SopAuthContext());
        }

        handleCaptcha(authIn);
        fillAuthInfo(authIn.getContext());
        handlePlatformInfo(authIn.getContext());

        return sopUserAuth.login(authIn);
    }

    public SopAuthUser loginDirect(SopAuthDirectIn authIn) {
        if (null == authIn.getContext()) {
            authIn.setContext(new SopAuthContext());
        }

        handleCaptcha(authIn);
        fillAuthInfo(authIn.getContext());
        handlePlatformInfo(authIn.getContext());

        return sopUserAuth.loginDirect(authIn);
    }

    public SopAuthResult authorize(SopAuthIn authIn) {
        if (null == authIn.getContext()) {
            authIn.setContext(new SopAuthContext());
        }

        handleCaptcha(authIn);
        fillAuthInfo(authIn.getContext());
        handlePlatformInfo(authIn.getContext());

        return sopUserAuth.authorize(authIn);
    }

    @Override
    public SopAuthResult authorizeDirect(SopAuthDirectIn authIn) {
        if (null == authIn.getContext()) {
            authIn.setContext(new SopAuthContext());
        }

        handleCaptcha(authIn);
        fillAuthInfo(authIn.getContext());
        handlePlatformInfo(authIn.getContext());

        return sopUserAuth.authorizeDirect(authIn);
    }

    /**
     * 对平台连接进行续期
     *
     * @param req 检测参数
     */
    public String renewPlatform(SopPlatHeartInfo req) {
        return sopPlatformAuth.renew(req);
    }

    /**
     * 刷新用户访问令牌
     *
     * @return 返回新的认证信息
     */
    public SopCredential refresh() {
        return sopUserAuth.refresh(uniClientTool.getRefreshToken(request, ""));
    }

    public void logout() {
        sopUserAuth.logout(uniClientTool.getAccessToken(request, ""));
    }

    @Override
    public void logoutUser(SopUserIdIn req) {
        sopUserAuth.logoutUser(req);
    }

    public void logoutSession() {
        sopUserAuth.logoutSession(uniClientTool.getAccessToken(request, ""));
    }
}
